<template>
    <div>
        <a-modal v-model:visible="modalVisible" title="确认无卡模式开机" @ok="handleOk" :confirm-loading="confirmLoading">
            <div class="instance-modal">
                <div>
                    无卡模式无GPU，配置为 1核心CPU，2GB内存
                </div>
                <!-- <div class="fz-14 mb-8 col-8A93A7">实例单价: ￥{{ instance_price || 0 }}/小时</div> -->
                <div class="fz-14 mb-8 col-8A93A7">实例单价: ￥ 0.1/小时</div>
                <div>开机后实例保存费用为 ￥{{ open_instance_storage_price || 0 }}/小时</div>
                <div class="fz-12 col-8A93A7">无卡启动时无法进行运算，可进行安装镜像环境等操作<span class="fz-14 col-money ml-8"><info-circle-filled
                            style="margin-right: 5px;" />开机48小时后自动关机</span></div>
            </div>
        </a-modal>
    </div>
</template>

<script setup>
import { defineProps, defineEmits, computed, ref } from 'vue';
import { handleInstance } from 'api/modules/api.instance'
import { storeToRefs } from 'pinia'
import userInfoStore from '@/stores/userInfo.js'
import { printGBSize } from '@/libs/util.common'
import {
    InfoCircleFilled
} from '@ant-design/icons-vue';
const useUserInfoStore = userInfoStore()
const { accountInfo: account } = storeToRefs(useUserInfoStore);

const props = defineProps({
    modelValue: {
        type: Boolean,
    },
    instance: {
        type: Object,
        default: {}
    },
    setting: {
        type: Array,
        default: []
    }
});

const emit = defineEmits(['update:modelValue', 'handelOpt', 'handelNoBalance']);
const modalVisible = computed({
    get: () => props.modelValue,
    set: v => emit('update:modelValue', v),
});

// 确定按钮的回调
const confirmLoading = ref(false)
const handleOk = () => {
    const params = {
        instanceId: props.instance?.Instance?.instanceId,
        method: 'start',
        subject: 'card_less_start'
    }
    confirmLoading.value = true
    handleInstance(params).then(res => {
        confirmLoading.value = false
        if (res.code == 0) {
            emit('update:modelValue', false)
            emit('handelOpt', props.instance?.Instance?.instanceId)
        }
    }).catch(err => {
        confirmLoading.value = false
        if (err.code == 10111) {
            emit('update:modelValue', false)
            emit('handelNoBalance', parseInt(instanceFee()) + parseInt(getStorageTotalFee()))
        }
    })
};

// 获取实例单价
const instance_price = ref('')
const getInstancePrice = () => {
    const filterItem = props.instance?.MachineSku.find(item => {
        return item.skuName == 'card_less'
    })
    instance_price.value = filterItem.price
}

getInstancePrice()

// 获取价格 开机保存价格open_instance_storage_price
const open_instance_storage_price = ref('')
const getPrice = () => {
    props.setting.forEach(item => {
        if (item.key == 'open_instance_storage_price') {
            open_instance_storage_price.value = item.value
        }
    })
}

getPrice()

// 费用金额 = 实例价格 + 存储价格
// 实例价格 = 机器sku价格 * 显卡数量 * 租用周期 * 用户实例折扣
const instanceFee = () => {
    let price = instance_price.value
    const duration = props.instance?.Order?.duration || 0
    const discount = account?.value?.userDiscount?.instanceDiscount || 1
    const countFee = price * 1 * duration * discount
    return countFee.toFixed(2) || 0
}

// 存储价格
const formatStorageTotalFee = ref(0)
const getStorageTotalFee = () => {
    const price = open_instance_storage_price.value || 0
    const storageNum = printGBSize(props.instance?.InstancePhysicalState?.systemDiskSize + props.instance?.InstancePhysicalState?.dataDiskSize)
    const countFee = price * storageNum
    formatStorageTotalFee.value = countFee.toFixed(2) || 0
}

getStorageTotalFee()

</script>

<style scoped lang="less">
.instance-modal {
    font-size: 14px;
    color: #646F87;

    &>div {
        margin-bottom: 8px;
    }
}
</style>

